import request from '@/utils/request'

export function add(data) {
  return request({
    url: 'api/v2/base/equip/equipmentTerminal/save',
    method: 'post',
    data
  })
}

export function del(ids) {
  return request({
    url: 'api/v2/base/equip/equipmentTerminal/del',
    method: 'delete',
    data: ids
  })
}

export function edit(data) {
  return request({
    url: 'api/v2/base/equip/equipmentTerminal/save',
    method: 'post',
    data
  })
}

export function getEqListByDealer(dealer_id) {
  const params = {
    dealer_id
  }
  return request({
    url: 'api/v2/base/equip/equipmentTerminal/getEquipmentListByDealer',
    method: 'get',
    params
  })
}


export function excelUpload(data) {
  // console.log("data",data)
  const formData = jsToFormData(data)
  // console.log("formData", formData)
  return request({
    url: 'api/v2/base/equip/equipmentTerminal/excelUpload',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime() }
  })
}

export function jsToFormData(data) { // 对象转formdata格式
  const formData = new FormData()
  const obj = data
  for (var i in obj) {
    // eslint-disable-next-line no-undef
    if (Array.isArray(obj[i])) {
      obj[i].map(item => {
        formData.append(i, item)
      })
    } else if (obj[i] instanceof FileList) {
      // filelist 文件类型的处理
      for (var fileItem = 0; fileItem < obj[i].length; fileItem++) {
        formData.append(i, obj[i].item(fileItem))
      }
    } else {
      formData.append(i, obj[i])
    }
  }
  return formData
}

export default { add, edit, del, getEqListByDealer,excelUpload }
